package com.randev.snap;

import java.util.Locale;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

public class SnapMain extends Activity {

	private final String TAG = "SnapMain";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        
        boolean firstTime = prefs.getBoolean("FirstTime", true);
        String localeCode = prefs.getString("Language", "en");
        boolean useDeviceLanguage = prefs.getBoolean("DeviceLanguage", false);
        
        if (!useDeviceLanguage) {
        	if (!(localeCode.isEmpty() && localeCode.contentEquals(""))) {
    			Locale locale = new Locale(localeCode);
    			Locale.setDefault(locale);
    			Configuration config = new Configuration();
    			config.locale = locale;
    			getBaseContext().getResources().updateConfiguration(config,
    			      getBaseContext().getResources().getDisplayMetrics());
    		}
        }
        
        if (firstTime) {
        	try {
        		Intent initializeIntent = new Intent(SnapMain.this, Initialize.class);
        		finish();
        		startActivity(initializeIntent);
        	} catch (ActivityNotFoundException e) {
        		Log.w(TAG, "Unable to start initialize activity: " + e.toString());
        	}
        } else {
        	try {
        		Intent loginActivityIntent = new Intent(SnapMain.this, LoginActivity.class);
				finish();
				startActivity(loginActivityIntent);
				
        	} catch (ActivityNotFoundException e) {
        		Log.w(TAG, "Unable to start login activity: " + e.toString());
        	}	
        }
	}
}
